﻿
<script type="text/html" template>
    <title>{{layui.setter.MenuName}}-{{layui.setter.name}}</title>
</script>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <script type="text/html" template>
            <a><cite>{{layui.setter.MenuName}}</cite></a>
        </script>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="key" id="key" placeholder="用户名/姓名/手机号" autocomplete="off" class="layui-input">
                    </div>
                    <!--<label class="layui-form-label">单位</label>
                    <div class="layui-input-inline" style="width:200px;">
                        <select name='groupID' id='groupID' lay-search lay-filter="selGroup">
                            <option value=""></option>
                        </select>
                    </div>-->
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-delete">
                        <i class="layui-icon layui-icon-delete layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <script type="text/html" template lay-url="{{ layui.setter.APIUrl }}/admin/userMgr/GetButtonRight?i=_Supplier"
                        lay-type="post" lay-done="layui.data.setActive()">
                    {{ d.Data }}
                </script>
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>

<script>

    layui.data.setActive = function () {

        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var view = layui.view;
            var table = layui.table;
            var active = {
                del: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    //return layer.msg('已删除');
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/admin/userMgr/BatchDelete' //实际使用请改成服务端真实接口
                            , data: { "ids": ids }
                            , type: "post"
                            //, success: function (res) {
                            //}
                            , done: function (res) {
                                layer.close(index); //执行关闭
                                layer.msg(res.ResultInfo);
                                table.reload('LAY-app-content-list');
                            }
                        });
                    });
                },
                //添加
                add: function (othis) {
                    admin.popup({
                        title: '添加信息'
                        , skin: 'layui-layer-lan'
                        , area: ['38%', '80%']
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('/admin/Portal_User_Supplier/add').done(function () {
                                form.render(null, 'layuiadmin-app-form-list');
                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                    var field = data.field; //获取提交的字段

                                    //if (!data.field.DepartmentID_SelSource) {
                                    //    layer.msg('请选择部门');
                                    //    return
                                    //}
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    //请求登入接口
                                    admin.req({
                                        url: layui.setter.APIUrl + '/admin/userMgr/Add' //实际使用请改成服务端真实接口
                                        , data: field
                                        , type: "post"
                                        , done: function (res) {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-app-content-list'); //重载表格
                                            layer.msg('新增成功');
                                        }
                                    });
                                });
                            });
                        }
                    });
                },
                updatepassword: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }

                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });

                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定重置密码吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/admin/userMgr/UpdatePassWord' //实际使用请改成服务端真实接口
                            , data: { "userpids": ids }
                            , type: "post"
                            , done: function (res) {
                                layer.close(index); //执行关闭
                                table.reload('LAY-app-content-list');
                                layer.msg('重置成功！');
                            }
                        });
                    });
                }
            };

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }


    layui.use(['admin', 'table'], function () {

        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form;


        //admin.req({
        //    url: layui.setter.APIUrl + '/admin/groupMgr/customerQuery2', //实际使用请改成服务端真实接口
        //    type: 'post',
        //    done: function (res) {
        //        layui.each(res.Data, function (index, eachdata) {
        //            $("#groupID").append("<option value='" + eachdata.id + "'>" + eachdata.title + "</option>");
        //        });
        //        form.render('select');
        //    }
        //});

        var varButtonRight = "";
        //文章管理
        table.render({
            elem: '#LAY-app-content-list'
            , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: ['filter']
            , method: "post"
            , url: layui.setter.APIUrl + '/admin/userMgr/listPainSupplier'
            , headers: { //通过 request 头传递
                Authorization: "bearer " + layui.data('layuiAdmin').access_token
            }
            , parseData: function (res) { //res 即为原始返回的数据
                varButtonRight = res.Data.ButtonRight;
                return {
                    "ResultCode": res.ResultCode, //解析接口状态
                    "ResultInfo": res.ResultInfo, //解析提示文本
                    "count": res.Data.TotalRecordCount, //解析数据长度
                    "Data": res.Data.Items //解析数据列表
                };
            }
            , cols: [[
                { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100
                { field: 'UserID', title: '用户名' },
                { field: 'UserName', title: '真实姓名' },
                { field: 'Mobile', title: '手机号', minWidth: 120 },
                { field: 'LastLoginTime', title: '最后登录时间', templet: '<div>{{ Format(d.LastLoginTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },
                {
                    field: 'State_Sel', title: '状态', templet: function (d) {
                        var State_Sel = d.State_Sel;
                        var strDes = '状态[0.启用|1.停用]';
                        var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
                        var lists = strDes.split('|');
                        if (lists[0].split('.')[0] == State_Sel) {
                            return '<button class="layui-btn layui-btn-xs">' + lists[0].split('.')[1] + "</button>";// layui-btn-primary
                        }
                        else {
                            return '<button class="layui-btn layui-btn-primary layui-btn-xs">' + lists[1].split('.')[1] + "</button>";// layui-btn-primary
                        }
                    }, width: 80, align: 'center'
                },
                { field: 'SupplierName', title: '供应商' },
                //{ field: 'GroupName', title: '所属组织' },
                {
                    title: '操作', minWidth: 250, fixed: 'right', align: 'center', templet: function (d) {
                        var strRtn = "";
                        if (varButtonRight.indexOf('view') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"view\">查看</a> ";
                        }
                        if (varButtonRight.indexOf('edit') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
                        }
                        if (varButtonRight.indexOf('del') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> ";
                        }
                        if (varButtonRight.indexOf('SetRole') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"SetRole\">角色</a> ";
                        }
                        return strRtn;
                    }
                }

            ]]
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
            }
        });

        //监听工具条
        table.on('tool(LAY-app-content-list)', function (obj) {
            //debugger;
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        url: layui.setter.APIUrl + '/admin/userMgr/BatchDelete' //实际使用请改成服务端真实接口
                        , data: { "ids": data.PID }
                        , type: "post"
                        //, success: function (res) {
                        //}
                        , done: function (res) {
                            layer.close(index); //执行关闭
                            table.reload('LAY-app-content-list');
                            layer.msg('删除成功');
                        }
                    });
                });
            }
            else if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑信息'
                    , area: ['38%', '80%']
                    , id: 'LAY-popup-content-edit'
                    , success: function (layero, index) {
                        view(this.id).render('/admin/Portal_User_Supplier/add', data).done(function () {
                            form.render(null, 'layuiadmin-app-form-list');

                            //监听提交
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: layui.setter.APIUrl + '/admin/userMgr/update' //实际使用请改成服务端真实接口
                                    , data: field
                                    , type: "post"
                                    , done: function (res) {
                                        layui.table.reload('LAY-app-content-list'); //重载表格
                                        layer.close(index); //执行关闭
                                        layer.msg('修改成功');
                                    }
                                });
                            });
                        });
                    }
                });
            }
            else if (obj.event === 'view') {
                admin.popup({
                    title: '查看信息'
                    , area: ['38%', '80%']
                    , id: 'LAY-popup-content-view'
                    , success: function (layero, index) {
                        view(this.id).render('/admin/Portal_User_Supplier/view', data).done(function () {
                            //监听提交
                            form.render(null, 'layuiadmin-app-form-list');
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            }
            else if (obj.event === 'SetRole') {
                admin.popup({
                    title: '设置角色'
                    , area: ['38%', '50%']
                    , id: 'LAY-popup-content-view'
                    , success: function (layero, index) {
                        view(this.id).render('/admin/Portal_User_Supplier/SetUserRole', data).done(function () {
                            //监听提交
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            }

        });


        form.render(null, 'app-content-list');

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            }
            );
        });

        form.on('submit(LAY-app-contlist-delete)', function (data) {
            $("#key").val("");
            //$("#usertype").val('');
            form.render('select');

            var field = data.field;
            field.key = "";
            //field.usertype = "";
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
    });
</script>